//
//  DPAlipayManager.swift
//  DoctorEPlus
//
//  Created by 王林波 on 2021/3/15.
//

import UIKit
import IDealist

class DPAlipayManager: NSObject {
    
    
    func goAlipayAuth()  {
        
        let authInfo = APAuthInfo.init()
        authInfo.appID = String.alipay_appId
        authInfo.pid = String.alipay_pid
        authInfo.authType = "AUTHACCOUNT"
        let authInfoStr = authInfo.description
        let manager = BHHTTPClient.init()
        manager.post(url: String.wallet_alipay_auth_bind_url, paramters: ["sign":authInfoStr]) { (success, errorMsg, responseDic) in
            if success == true {
                let signStr = BHJsonTool.decodeStringFromDic(dic: responseDic, key: "result")
                if signStr.isEmpty == false { // 有加密
                    let resultStr = authInfoStr+"&sign="+signStr+"&sign_type=RSA2"
                    AlipaySDK.defaultService()?.auth_V2(withInfo: resultStr, fromScheme: String.appScheme, callback: { (resultHashDic) in
                        
                    })
                }else {
                    BHToast.toast(text: "获取授权信息失败")
                }

            }else {
                BHToast.toast(text: "获取授权信息失败")
            }
        }
    }
    
    
    // 绑定支付宝
    func bindAlipay(_ token: String) {
        let manager = BHHTTPClient.init()
        manager.post(url: String.wallet_alipay_bind_url, paramters: ["accessToken":token]) { (success, errorMsg, responseDic) in
            if success == true {
                BHToast.toast(text: "绑定成功")
                NotificationCenter.default.post(name: NSNotification.Name.init(String.alipayBindResulyNotificationKey), object: nil)
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
     
    }
}



// 输入框输入的文字是否是合格的钱数
extension DPAlipayManager {
    // 返回正确的文本,是否可编辑
    static func judgeTextAvaliable(_ text: String, _ rangeText: String, _ maxText: String) -> String{
        if rangeText == "" { // 删文字
            if text.count > 1 {
                return text.id_subString(to: text.count-2)
            }else {
                return ""
            }
        }
        else if rangeText == "." { // 输入小数点
            if text.isEmpty == true {
                return "0."
            }else if text.contains(".") {
                return text
            }
            else {
                return text+rangeText
            }
        }
        else {
            if text.contains(".") {
                let pointLast = text.components(separatedBy: ".")
                let lastText = pointLast.last
                if lastText != nil {
                    if lastText!.count == 2 {
                        return text
                    }
                }
            }
            
            if rangeText == "0" {
                if text == "0" {
                    return "0"
                }
            }
            
            let resultText = text+rangeText
            let decimalInput = Decimal.init(string: resultText)!
            let decimalMax = Decimal.init(string: maxText)!
            
            let result = decimalInput.isLess(than: decimalMax)
            if result == true {
                return resultText
            }else {
                return maxText
            }
        }
    }
    
    static func checkMoneyAvalid(_ inputMoney: String) -> (Bool,String) {
        let decimalInput = Decimal.init(string: inputMoney)!
        if BHHTTPEnvironment.shared.apiEnvironment == .Realese {
            let decimalMax = Decimal.init(string: "100")!
            let result = decimalInput.isLess(than: decimalMax)
            if result == true {
                return (false,"提现金额不能小于100元")
            }else {
                return (true,"")
            }
        }else {
            let decimalMax = Decimal.init(string: "0.1")!
            let result = decimalInput.isLess(than: decimalMax)
            if result == true {
                return (false,"测试数据提现金额不能小于0.1元")
            }else {
                return (true,"")
            }
        }
    }
}
